package com.itheima.controller;

import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学生控制器
 * @author GY
 * @version V1.0
 * @date 2025-01-14 11:37
 */
@Slf4j
@RequestMapping("/students")
@RestController
@RequiredArgsConstructor
public class StudentController {
    private final StudentService studentService;

    /**
     * 4.1学员列表查询
     */
    @GetMapping
    public Result pageQuery(Student student){
        log.info("student:{}",student);
        PageResult<Student> students = studentService.pageQuery(student);
        return Result.success(students);
    }

    /**
     * 4.2删除学员
     */
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable("ids") List<Integer> ids){
        log.info("ids:{}", ids);
        studentService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 4.3添加学员
     */
    @PostMapping
    public Result save(@RequestBody Student student){
        log.info("PostMapping student:{}",student);
        studentService.save(student);
        return Result.success();
    }

    /**
     * 4.4根据ID查询
     */
    @GetMapping("/{id}")
    public Result queryById(@PathVariable("id") Integer id){
        log.info("id:{}",id);
        Student student = studentService.queryById(id);
        return Result.success(student);
    }

    /**
     * 4.5修改学员
     */
    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("PutMapping student:{}",student);
        studentService.update(student);
        return Result.success();
    }

    /**
     * 4.6违纪处理
     */
    @PutMapping("/violation/{id}/{score}")
    public Result deductPointsById(@PathVariable("id") Integer id,@PathVariable("score") Integer score){
        log.info("id:{},scpre:{}",id,score);
        studentService.deductPointsById(id,score);
        return Result.success();
    }
}
